//
//  NoteAddViewController.m
//  SmileRead
//
//  Created by dllo on 15/8/31.
//  Copyright (c) 2015年 JIZ. All rights reserved.
//

#import "NoteAddViewController.h"
#import "Path.h"
#import "FileNameList.h"
#import "AppDelegate.h"

@interface NoteAddViewController ()<UITextViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>

@property (nonatomic, retain) UITextView *textView;

@property (nonatomic, retain) UIView *underView;

// 确定按钮
@property (nonatomic, retain) UIButton *button;

@property (nonatomic, retain) NSMutableArray *arrayOfnote;

@property (nonatomic, retain) UIImageView *backImageView;

@property (nonatomic, retain) UIVisualEffectView *effectView;

@property (nonatomic, retain) UILabel *titlabel;

@property (nonatomic, retain) UIButton *buttonOfImage;

@property (nonatomic, retain) UIImagePickerController *picker;

@property (nonatomic, retain) UIImageView *imageOfcamera;

@property (nonatomic, retain) NSMutableDictionary *dic;

@end

@implementation NoteAddViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.arrayOfnote = [NSMutableArray array];
        
        self.dic = [NSMutableDictionary dictionary];
        self.imageOfcamera = [[UIImageView alloc] init];
        NSString *path = [Path pathOfLibraryWithFileName:FILE_Note];
        NSFileManager *file = [NSFileManager defaultManager];
        if ([file fileExistsAtPath:path]) {
            self.arrayOfnote = [NSMutableArray arrayWithContentsOfFile:path];
        }
        else {
            [self.arrayOfnote writeToFile:path atomically:YES];
        }

    }
        return self;
}

- (void)dealloc
{
    [_imageOfcamera release];
    [_picker release];
    [_buttonOfImage release];
    [_titlabel release];
    [_effectView release];
    [_backImageView release];
    [_arrayOfnote release];
    [_underView release];
    [_textView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSubView];
}

- (void)createSubView
{
    self.backImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.backImageView.image = [UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"4.jpg"]];
    [self.view addSubview:self.backImageView];
    [_backImageView release];
    
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];
    self.effectView = [[UIVisualEffectView alloc] initWithEffect:blur];
    self.effectView.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [self.view addSubview:self.effectView];
    [_effectView release];
    
    self.titlabel = [[UILabel alloc] initWithFrame:CGRectMake(170 * widthFit, 64 + 10 * heightFit, self.view.frame.size.width - 180 * widthFit, 130 * heightFit)];
    //self.titlabel.backgroundColor = [UIColor greenColor];
    self.titlabel.font = [UIFont systemFontOfSize:25];
    NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"记录你的心情" attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
    self.titlabel.attributedText = string;
    [self.effectView.contentView addSubview:self.titlabel];
    [_titlabel release];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10 * widthFit, 64 + 10 * heightFit, 150 * widthFit, 130 * heightFit)];
    imageView.layer.cornerRadius = 30 * widthFit;
    imageView.layer.masksToBounds = YES;
    imageView.image = [UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"xie.jpg"]];
    [self.effectView.contentView addSubview:imageView];
    [imageView release];
    
    
    self.textView = [[UITextView alloc] initWithFrame:CGRectMake(0, 64 + 64 * heightFit + 150 * heightFit, self.view.frame.size.width, self.view.frame.size.height - 64 - 150 * heightFit - 64 * heightFit)];
    self.textView.delegate = self;
    self.textView.font = [UIFont systemFontOfSize:17];
    self.textView.backgroundColor = [UIColor cyanColor];
    [self.view addSubview:self.textView];
    [_textView release];
    
    self.underView = [[UIView alloc] initWithFrame:CGRectMake(0, 64 + 150 * heightFit, self.view.frame.size.width, 64 * heightFit)]; 
    self.underView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.underView];
    [_underView release];
    
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width / 2 - 20 * widthFit, 12 * heightFit, 40 * widthFit, 40 * widthFit)];
    [self.button setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"queding.png"]] forState:UIControlStateNormal];
    self.button.layer.cornerRadius = self.button.frame.size.height / 2;
    [self.button addTarget:self action:@selector(finsh) forControlEvents:UIControlEventTouchUpInside];
    self.button.layer.masksToBounds = YES;
    //self.button.backgroundColor = [UIColor greenColor];
    [self.underView addSubview:self.button];
    [_button release];
    
    self.buttonOfImage = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 80 * widthFit, 12 * heightFit, 40 * widthFit, 40 * widthFit)];
    [self.buttonOfImage setBackgroundImage:[UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"xiangji.png"]] forState:UIControlStateNormal];
    [self.buttonOfImage addTarget:self action:@selector(camera) forControlEvents:UIControlEventTouchUpInside];
    [self.underView addSubview:self.buttonOfImage];
    [_buttonOfImage release];
    
    self.picker = [[[UIImagePickerController alloc] init] autorelease];
    self.picker.delegate = self;
    self.picker.allowsEditing = YES;
    
    self.imageOfcamera.frame = CGRectMake(40 * widthFit, 12 * heightFit, 40 * widthFit, 40 * widthFit);
    self.imageOfcamera.image = [UIImage imageWithContentsOfFile:[Path pathOfImageWithName:@"zhaopian.png"]];
    [self.underView addSubview:self.imageOfcamera];
    //self.imageOfcamera.backgroundColor = [UIColor greenColor];
    [_imageOfcamera release];
    
}

// 从本地取出照片
- (void)camera
{
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [tempAppDelegate.window.rootViewController presentViewController:self.picker animated:YES completion:^{
        
    }];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    self.imageOfcamera.image = [info objectForKey:UIImagePickerControllerEditedImage];
    NSString *path = [Path pathOfLibraryWithFileName:FILE_Note];
    if (self.arrayOfnote.count > 7) {
        [self.arrayOfnote removeObjectAtIndex:0];
    }
    if (self.imageOfcamera.image == [info objectForKey:UIImagePickerControllerEditedImage]) {
        NSData *imageData = UIImagePNGRepresentation(self.imageOfcamera.image);
        [self.dic setObject:imageData forKey:@"image"];
    }
    //[self.dic setObject:datestring forKey:@"time"];
    [self.dic setObject:self.textView.text forKey:@"note"];
    //[self.arrayOfnote addObject:self.dic];
    [self.arrayOfnote writeToFile:path atomically:YES];
    [self.picker dismissViewControllerAnimated:YES completion:^{
    }];
}



// 编辑完成后存放到文件中
- (void)textViewDidEndEditing:(UITextView *)textView
{
    // 获取前时间
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd HH:mm"];
    NSDate *now = [NSDate date];
    NSString *datestring = [dateFormatter stringFromDate:now];
    [self.dic setObject:textView.text forKey:datestring];
    NSString *path = [Path pathOfLibraryWithFileName:FILE_Note];
    
    // 将数据放入字典中
    if (self.arrayOfnote.count > 7) {
        [self.arrayOfnote removeObjectAtIndex:0];
    }
    /*
    if (self.imageOfcamera.image != nil) {
        NSData *imageData = UIImagePNGRepresentation(self.imageOfcamera.image);
        [self.dic setObject:imageData forKey:@"image"];
    }
     */
    [self.dic setObject:datestring forKey:@"time"];
    [self.dic setObject:textView.text forKey:@"note"];
    [self.arrayOfnote addObject:self.dic];
    [self.arrayOfnote writeToFile:path atomically:YES];
}

// 编辑完成
- (void)finsh
{
    [self.view endEditing:NO];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
